$(function () {
    $("#mobile").val("");
    $("#code").val("");
    getUrl("retrievePassword/selectorMode");
    //实时监控输入框
    inputChange("#mobile","#telError");
    inputChange("#code","#codeError");
    function telMessage(){
        var mobile = $("#mobile").val();
        if (mobile == "" || mobile == null) {
            validateSpanMsg("telError","请输入手机号");
            return false;
        } else if (!isMobile(mobile)) {
            validateSpanMsg("telError","手机号码格式错误");
            return false;
        }else{
            return true;
        }
    }
    function codeMessage(){
        var code = $("#code").val();
        if (code == "" || code == null) {
            validateSpanMsg("codeError","请输入验证码");
            return false;
        }else if(!/^\d{6}$/.test(code)){
            validateSpanMsg("codeError","验证码错误");
            return false;
        }else{
            return true;
        }
    }
    $("#mobile").blur(function(){
        telMessage();
    })
    $("#code").blur(function(){
        codeMessage();
    })
    $("#get").click(function () {
        var mobile = $("#mobile").val();
        if(telMessage()){
            $("#get").attr("disabled", "disabled");
            $(this).val("发送中...");
            ajaxGetCodeToMobile(mobile, this);
        }
    });
    $("#sub").click(function () {
        var code = $("#code").val();
        var mobile = $("#mobile").val();
        if (telMessage()&&codeMessage()){
            $("#sub").attr("disabled", "disabled");
            ajaxValidateCodeOfMobile(code);
        }
    });
});


/**
 * 发送手机验证码
 * @param mobile    用户手机号
 */
function ajaxGetCodeToMobile(mobile, that) {
    if (isMobile(mobile)) {
        var url = "/Lvscore_User_M/ajaxGetCodeToMobile";
        $.post(
            url,
            {'mobile': mobile},
            function (data) {
                $("#get").removeAttr("disabled", "disabled");
                if (data == 1) {
                    settime(that);
                    alertBox("发送成功,请注意查收");
                } else {
                    setTimeout(function(){
                        validateSpanMsg("telError","您的手机号未注册！");
                        that.removeAttribute("disabled");
                        that.value = "获取验证码";
                        that.style.opacity = "1";
                    },1000);
                }
            });
    } else {
        validateSpanMsg("telError","手机号码格式错误");
    }
}

/**
 * 手机方式重置密码验证码校验
 * @param code    用户输入验证码
 */
function ajaxValidateCodeOfMobile(code) {
    if (/^\d{6}$/.test(code)) {
        var url = "/Lvscore_User_M/ajaxValidateCodeOfMobile";
        $.post(
            url,
            {'code': code},
            function (data) {
                $("#sub").removeAttr("disabled", "disabled");
                if (data == 1) {
                    window.location.href = "/Lvscore_User_M/userWeb/views/retrievePassword/resetPhone.html";
                } else {
                    validateSpanMsg("codeError","验证码输入错误");
                }
            });
    } else {
        $("#sub").removeAttr("disabled", "disabled");
        validateSpanMsg("codeError","验证码输入错误");
    }

}